package com.xiaoyu.lanling.feature.user.options

import android.content.Context
import androidx.appcompat.app.AlertDialog
import com.xiaoyu.im.provider.ChatIdMapper
import com.xiaoyu.im.provider.ConversationManager
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.user.data.UserInfoData

class BlackAction(context: Context, val requestTag: Any, private val targetUid: String, private val blacked: Boolean) : MoreActionItemBase(context) {

    private val chatId: String? = ChatIdMapper.getInstance().getChatIdByUid(targetUid)

    override val title: CharSequence
        get() = mContext.getString(if (blacked) R.string.user_more_action_unblack else R.string.user_more_action_black)

    override fun onClick() {
        if (blacked) {
            UserInfoData.blackUser(requestTag, targetUid, false)
            ConversationManager.getInstance().blackAsync(chatId, false)
        } else {
            AlertDialog.Builder(mContext)
                    .setMessage(R.string.user_black_dialog)
                    .setNegativeButton(R.string.action_cancel) { _, _ -> }
                    .setPositiveButton(R.string.action_confirm) { _, _ ->
                        UserInfoData.blackUser(requestTag, targetUid, true)
                        ConversationManager.getInstance().blackAsync(chatId, true)
                    }
                    .show()
        }
    }
}